<?php include('includes/before_login.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>

        <meta http-equiv="content-type" content="text/html; charset=utf-8" />

        <meta name="description" content="" />

        <meta name="keywords" content="" />

        <meta name="author" content="" />

        <link rel="stylesheet" type="text/css" href="style.css" media="screen" />

        <title><?php echo $heading ?></title>

    </head>

    <body>

        <div id="wrapper">

            <?php include('includes/header.php'); ?>

            <?php include('includes/nav.php'); ?>

            <div id="content">                
                <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
                    <tr>
                        <form name="form1" method="post" action="check_login.php">
                            <td>
                                <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                                    <tr>
                                        <td colspan="3"><strong>Member Login </strong></td>
                                    </tr>
                                    <tr>
                                        <td width="78">RUID</td>
                                        <td width="6">:</td>
                                        <td width="294"><input name="myusername" type="text" id="myusername"></input></td>
                                    </tr>
                                    <tr>
                                        <td>Password</td>
                                        <td>:</td>
                                        <td><input name="mypassword" type="password" id="mypassword"></input></td>
                                    </tr>
                                    <tr>
                                        <td>&nbsp;</td>
                                        <td>&nbsp;</td>
                                        <td><input type="submit" name="Submit" value="Login"/></td>
                                    </tr>
                                    <?php
                                    if ($_GET["status"] == "0")
                                        echo "<tr><td colspan='3'>Invalid RUID and/or password</td></tr>";
                                    ?>
                                </table>
                            </td>
                        </form>
                    </tr>
                </table>

                <!--Form for registering-->
                <br><br>
                        <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
                            <tr><td>
                                    <form action="main_login.php" method="post">
                                        <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                                            <tr><td colspan="2"><h4>New User? Register Below. </h4></td></tr>

                                            <?php
                                            $required = array('RUID', 'Password', 'FirstName', 'LastName', 'Email', 'NetID');
                                            $db = mysql_connect(db_server, $db_user, $db_password);
                                            mysql_select_db($db_dbname);
                                            $result = mysql_query("describe Users");
                                            while ($row = mysql_fetch_array($result)) {
                                                if (in_array($row['Field'], $required)) {
                                                    echo '<tr><td><font color="red">*' . $row['Field'] . ':</font></td><td><input type="text" name="' . $row['Field'] . '" /></td></tr>';
                                                } else {
                                                    echo '<tr><td>' . $row['Field'] . ':</td><td><input type="text" name="' . $row['Field'] . '" /></td></tr>';
                                                }
                                            }
                                            echo '<tr><td></td><td><input type="submit" name="Submit" value="Register"/></td></tr>';
                                            ?>
                                        </table>
                                    </form></td></tr>
                        </table>
                       
                                                <?php
                                                if (!empty($_POST['RUID'])) {
                                                    $table = 'Users';
                                                    $submit = 0;
                                                    $result = mysql_query("describe $table");
                                                    $colnames = '';
                                                    $values = '';
                                                    $update = '';
                                                    while ($row = mysql_fetch_array($result)) {
                                                        if (!in_array($row['Field'], $excluded)) {
                                                            $colnames.=$row['Field'] . ",";
                                                            $val = $_POST[$row['Field']];
                                                            if (!empty($val) || $val != '') {
                                                                $submit = 1;
                                                                $values.= "'" . $val . "',";
                                                            } else {
                                                                $values.="NULL,";
                                                            }
                                                        }
                                                    }
                                                    $values = substr($values, 0, strlen($values) - 1);
                                                    $colnames = substr($colnames, 0, strlen($colnames) - 1);
                                                    $sql = "INSERT INTO $table ($colnames) VALUES ($values)";

                                                    if ($submit == 1) {
                                                        echo '<br><br><table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
                            <tr><td>
                                    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                                        <tr><td>';
                                                        echo "SQL QUERY: " . $sql . "<BR><BR>";
                                                        $result = mysql_query($sql);
                                                        if ($result)
                                                            echo "<strong>You have been registered.  Please log in above.</strong><br><br>";
                                                        else
                                                            echo "<strong>ERROR: " . mysql_error() . "</strong><br><br>";
                                                         echo "</td></tr> </table></td></tr></table>";
                                                    }
                                                }
                                               
                                                ?>
                        </div> <!-- end #content -->

                        <?php include('includes/sidebar.php'); ?>

                        <?php include('includes/footer.php'); ?>

                        </div> <!-- End #wrapper -->

                        </body>

                        </html>